﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace Santa_sAdventure.Model
{
    class Player
    {
        // Player animations
        private Animation m_idleAnimation;
        private Animation m_runAnimation;
        private Animation m_jumpAnimation;
        private Animation m_dieAnimation;
        private AnimationPlayer m_animationPlayer;
        private SpriteEffects flip = SpriteEffects.None;
        // Position
        Vector2 m_position;
        // Speed
        Vector2 m_velocity;
        // Jumpspeed
        float m_jumpSpeed = -5.7f;
        // Lifes left
        private int m_lifes;
        // Sounds
        private SoundEffect m_walkingSound;
        private SoundEffectInstance m_walkingSoundInstance;

        // Construct
        internal Player(Texture2D a_playerRunTexture, Texture2D a_playerJumpTexture, Texture2D a_playerIdleTexture, Texture2D a_playerDieTexture, SoundEffect a_walkingSound)
        { 
            // Set player size
            Size = new Vector2(0.8f, 1.8f);
            CanJump = true;
            m_velocity = new Vector2(0, 0);
            Acceleration = new Vector2(0, 4);
            m_lifes = 3;

            // Set animations
            m_runAnimation = new Animation(a_playerRunTexture, 1, true, 10);
            m_jumpAnimation = new Animation(a_playerJumpTexture, 1, false, 11);
            m_idleAnimation = new Animation(a_playerIdleTexture, 1, true, 1);
            m_dieAnimation = new Animation(a_playerDieTexture, 1, false, 11);
            m_animationPlayer = new AnimationPlayer(m_idleAnimation);

            // Sound
            m_walkingSound = a_walkingSound;
            m_walkingSoundInstance = m_walkingSound.CreateInstance();
        }

        // Player position
        internal Vector2 Position { get { return m_position; } set { m_position = value; } }
        // Player velocity
        internal Vector2 Velocity { get { return m_velocity; } set { m_velocity = value; } }
        // Acceleration
        internal Vector2 Acceleration { get; set; }
        // Has fifes left
        internal bool NoLifesLeft { get { return m_lifes <= 0; } }
        // Nr of lives left
        internal int NrOfLifes { get { return m_lifes; } }

        // Bolleans
        private bool m_isAlive = true;
        // Player alive?
        internal bool IsALive()
        {
            return m_isAlive;
        }

        // Jump speed
        internal float JumpSpeed { get { return m_jumpSpeed; } }

        // Player size
        internal Vector2 Size{ get; set; }

        // Player startposition
        internal Vector2 StartPosition { get; set; }

        // If Player can jump
        internal bool CanJump { get; set; }

        // Make the player jump
        internal void Jump()
        {
            // Check
            if (CanJump)
            {
                CanJump = false;
                m_velocity.Y = m_jumpSpeed;

                // Play animation
                m_animationPlayer.PlayAnimation(m_jumpAnimation);
            }            
        }

        // Cancel jump
        private void CancelJump()
        {
            m_velocity.Y = 4;
        }

        // Make the player move left
        internal void MoveLeft(float a_elapsedTimeSeconds)
        {
            m_velocity.X = -4;

            // Play animation
            if (CanJump)
            {
                m_animationPlayer.PlayAnimation(m_runAnimation);
            }

            if (CanJump)
            {
                // If sound is not playing, play
                if (m_walkingSoundInstance.State != SoundState.Playing)
                {
                    m_walkingSoundInstance.Play();
                }
            } 
        }

        // Make the player move right
        internal void MoveRight(float a_elapsedTimeSeconds)
        {
            m_velocity.X = 4;

            // Play animation
            if (CanJump)
            {
                m_animationPlayer.PlayAnimation(m_runAnimation);
            }

            if (CanJump)
            {
                // If sound is not playing, play
                if (m_walkingSoundInstance.State != SoundState.Playing)
                {
                    m_walkingSoundInstance.Play();
                }
            } 
        }

        // Update player position
        internal void Update(float a_elapsedTimeSeconds)
        {
            m_position = Position + Velocity * a_elapsedTimeSeconds;
            m_velocity = Velocity + Acceleration * 2  * a_elapsedTimeSeconds;
        }

        // Kill the player
        internal void KillPlayer()
        {
            m_animationPlayer.PlayAnimation(m_dieAnimation);
            m_isAlive = false;
            m_lifes--;
        }

        // Respawn the player
        internal void Respawn()
        {
            CancelJump();
            m_animationPlayer.PlayAnimation(m_idleAnimation);
            Position = StartPosition;
            m_isAlive = true;
        }


        // Return animationplayer
        internal AnimationPlayer GetAnimationPlayer()
        {
            return m_animationPlayer;
        }

        // Return spriteeffects
        internal SpriteEffects GetSpriteEffect()
        {
            // Flip the sprite to face the way we are moving.
            if (Velocity.X > 0)
                flip = SpriteEffects.FlipHorizontally;
            else if (Velocity.X < 0)
                flip = SpriteEffects.None;

            return flip;
        }

        // Stand still
        internal void StandStill()
        {
            Velocity = new Vector2(0, Velocity.Y);
            // Play animation 
            if (CanJump)
            {
                m_animationPlayer.PlayAnimation(m_idleAnimation);
            }
            m_walkingSoundInstance.Stop();
        }

        // Reset variables
        internal void CreateNewPlayer()
        {
            CanJump = false;
            m_lifes = 3;
        }
    }
}
